package pt.iscte.dcti.pa.view;

import java.text.Format.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.ListIterator;
import java.util.Observable;
import java.util.Observer;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.omg.CORBA.PRIVATE_MEMBER;

import java.lang.reflect.*;

import javax.swing.text.html.HTMLDocument.Iterator;

public class DynamicTable extends Composite {
	
	private Table table;
	private Observer observer;
	
	public DynamicTable(Composite parent) {
		
		super(parent, SWT.NONE);
		
		setLayout(new GridLayout());
		table = new Table (this, SWT.MULTI | SWT.BORDER | SWT.FULL_SELECTION);
		table.setLinesVisible (true);
		table.setHeaderVisible (true);
		GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
		data.heightHint = 200;
		table.setLayoutData(data);
		load(null);
	}
	
	public void load(VisibleElement[][] sheet) {
		if (sheet != null) {
									 					
			try {
				char currentColumn = 'A';
				for (int i = 0; i < sheet[0].length; i++) {	//numero de colunas			
					new TableColumn(table, SWT.NONE).setText(Character.toString((currentColumn++)));
				}
			
				int i;
				TableItem tableItem;						
				
				//para cada linha
				for (VisibleElement[] strings : sheet) {
					//para cada coluna
					i = 0;
					tableItem = new TableItem(table, SWT.NONE);
					for (VisibleElement vElement : strings) {
						tableItem.setText(i++, vElement.getValue() == null ? "" : vElement.getValue());					
					}
				}
				
				for (int j = 0; j < sheet[0].length ; j++) {
					table.getColumn(j).pack();
				}
			}
			catch (Exception ex) {
				String exMessage = ex.getMessage();
				System.out.println("Excepcao: " + ex.getMessage());
			}
		}
	}
	
	public Table getTable() {
		return table;
	}
}
